package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.operamask.OperaMaskConfig;
import com.yanqu.road.entity.activity.operamask.OperaMaskGameData;
import com.yanqu.road.entity.activity.operamask.OperaMaskSceneData;
import com.yanqu.road.entity.activity.operamask.OperaMaskUserData;
import com.yanqu.road.entity.activity.operamask.config.OperaMaskSceneConfig;
import com.yanqu.road.entity.activity.operamask.config.OperaMaskSkillConfig;
import com.yanqu.road.entity.activity.operamask.game.OperaMaskBlock;
import com.yanqu.road.entity.activity.operamask.game.OperaMaskSkill;
import com.yanqu.road.entity.activity.operamask.sanxiao.OperaMaskBoom;
import com.yanqu.road.entity.activity.operamask.sanxiao.OperaMaskSkillEffect;
import com.yanqu.road.entity.enums.eOperaMaskBlockType;
import com.yanqu.road.entity.enums.eOperaMaskBoomType;
import com.yanqu.road.pb.activity.OperaMaskProto;
import com.yanqu.road.server.manger.activity.operamask.OperaMaskMgr;

import java.util.List;
import java.util.Map;

public class OperaMaskPb {

    public static OperaMaskProto.OperaMaskSyncConfigRespMsg.Builder parseOperaMaskSyncConfigRespMsg(OperaMaskConfig config) {
        OperaMaskProto.OperaMaskSyncConfigRespMsg.Builder builder = OperaMaskProto.OperaMaskSyncConfigRespMsg.newBuilder();
        for (OperaMaskSceneConfig sceneConfig : config.getSceneConfigMap().values()) {
            builder.addSceneConfig(parseOperaMaskSceneConfigMsg(sceneConfig));
        }
        for (List<OperaMaskSkillConfig> skillConfigs : config.getSkillConfigMap().values()) {
            for (OperaMaskSkillConfig skillConfig : skillConfigs) {
                builder.addSkillConfig(parseOperaMaskSkillConfigMsg(skillConfig));
            }
        }
        return builder;
    }

    public static OperaMaskProto.OperaMaskSkillConfigMsg.Builder parseOperaMaskSkillConfigMsg(OperaMaskSkillConfig config) {
        OperaMaskProto.OperaMaskSkillConfigMsg.Builder builder = OperaMaskProto.OperaMaskSkillConfigMsg.newBuilder();
        builder.setSkillId(config.getSkillId());
        builder.setLevel(config.getSkillLevel());
        builder.setSkillParam(config.getSkillParam());
        builder.setIcon(config.getIcon());
        return builder;
    }

    public static OperaMaskProto.OperaMaskSceneConfigMsg.Builder parseOperaMaskSceneConfigMsg(OperaMaskSceneConfig config) {
        OperaMaskProto.OperaMaskSceneConfigMsg.Builder builder = OperaMaskProto.OperaMaskSceneConfigMsg.newBuilder();
        builder.setSceneId(config.getSceneId());
        builder.setUnlock(config.getUnlock());
        builder.setUnlockSkills(config.getUnlockSkills());
        builder.setPaintBox(config.getPaintBox());
        builder.setStepNum(config.getStepNum());
        builder.setAssessLimit(config.getAssessLimit());
        builder.setEffectParam(config.getEffectParam());
        builder.setResId(config.getResId());
        builder.setSkillRating(config.getSkillRating());
        builder.setReward(config.getReward());
        return builder;
    }

    public static OperaMaskProto.OperaMaskUserDataMsg.Builder parseOperaMaskUserDataMsg(OperaMaskUserData data) {
        OperaMaskProto.OperaMaskUserDataMsg.Builder builder = OperaMaskProto.OperaMaskUserDataMsg.newBuilder();
        for (Map.Entry<Integer, Integer> entry : data.getSkillMaxLevelMap().entrySet()) {
            builder.addUnlockSkill(parseOperaMaskSkillMsg(entry.getKey(), entry.getValue()));
        }
        builder.setSingLevel(data.getSingLevel());
        OperaMaskGameData gameData = data.getGameData();
        builder.setSceneId(gameData != null ? gameData.getSceneId() : 0);
        builder.setEnergy(data.getEnergy());
        builder.setRecoveryTime(data.getRecoveryTime());
        for (OperaMaskSceneData sceneData : data.getSceneDataMap().values()) {
            builder.addScene(parseOperaMaskSceneMsg(sceneData));
        }
        builder.setBuySliverToken(data.getTokenList().contains(1) ? 1 : 0);
        builder.setBuyGoldToken(data.getTokenList().contains(2) ? 1 : 0);
        return builder;
    }

    public static OperaMaskProto.OperaMaskSceneMsg.Builder parseOperaMaskSceneMsg(OperaMaskSceneData data) {
        OperaMaskProto.OperaMaskSceneMsg.Builder builder = OperaMaskProto.OperaMaskSceneMsg.newBuilder();
        builder.setSceneId(data.getId());
        builder.setTopScore(data.getScore());
        builder.setStatus(data.getStatus());
        return builder;
    }

    public static OperaMaskProto.OperaMaskGameDataMsg.Builder parseOperaMaskGameDataMsg(OperaMaskGameData data, OperaMaskUserData userData) {
        OperaMaskProto.OperaMaskGameDataMsg.Builder builder = OperaMaskProto.OperaMaskGameDataMsg.newBuilder();
        for (List<OperaMaskBlock> list : data.getBlockList()) {
            for (OperaMaskBlock block : list) {
                OperaMaskProto.OperaMaskBlockMsg.Builder blockMsg = parseOperaMaskBlockMsg(block);
                builder.addBlockList(blockMsg);
            }
        }
        for (OperaMaskSkill skill : data.getSkillList()) {
            builder.addSkill(parseOperaMaskSkillMsg(skill));
        }
        builder.setBoxLevel(data.getBoxLevel());
        builder.setBoxValue(data.getBoxValue());
        builder.setScore(data.getScore());
        builder.setUseStep(data.getUseStep());
        builder.setLeftStep(data.getLeftStep());
        builder.addAllChooseSkill(data.getChooseSkill());
        builder.setAddStep(OperaMaskMgr.getBoxUpgradeAddStep(data));
        builder.setCanRandomSkillNum(OperaMaskMgr.getAllCanRandomSkillList(userData, data).size());
        builder.setRandomCount(data.getRandomTimes());
        builder.setSkipCount(data.getSkipTimes());
        builder.setSkillAddStep(data.getSkillAddStep());
        return builder;
    }

    public static OperaMaskProto.OperaMaskSkillMsg.Builder parseOperaMaskSkillMsg(OperaMaskSkill data) {
        return parseOperaMaskSkillMsg(data.getSkillId(), data.getSkillLevel());
    }

    public static OperaMaskProto.OperaMaskSkillMsg.Builder parseOperaMaskSkillMsg(int skillId, int level) {
        OperaMaskProto.OperaMaskSkillMsg.Builder builder = OperaMaskProto.OperaMaskSkillMsg.newBuilder();
        builder.setSkillId(skillId);
        builder.setLevel(level);
        return builder;
    }

    public static OperaMaskProto.OperaMaskBlockMsg.Builder parseOperaMaskBlockMsg(OperaMaskBlock data) {
        OperaMaskProto.OperaMaskBlockMsg.Builder builder = OperaMaskProto.OperaMaskBlockMsg.newBuilder();
        builder.setX(data.getX());
        builder.setY(data.getY());
        builder.setColor(getClientColor(data));
        builder.setScore(data.getScore());
        builder.setScoreMulti2(data.getTotalMul());
        return builder;
    }

    public static OperaMaskProto.OperaMaskBoomMsg.Builder parseOperaMaskBoomMsg(OperaMaskBoom data) {
        OperaMaskProto.OperaMaskBoomMsg.Builder builder = OperaMaskProto.OperaMaskBoomMsg.newBuilder();
        builder.setX(data.getX());
        builder.setY(data.getY());
        builder.setType(getClientBoomType(data));
        for (OperaMaskBlock block : data.getBlockList()) {
            builder.addBlockList(parseOperaMaskBlockMsg(block));
        }
        return builder;
    }

    public static OperaMaskProto.OperaMaskSkillEffectMsg.Builder parseOperaMaskSkillEffectMsg(OperaMaskSkillEffect data) {
        OperaMaskProto.OperaMaskSkillEffectMsg.Builder builder = OperaMaskProto.OperaMaskSkillEffectMsg.newBuilder();
        builder.setSkillId(data.getSkillId());
        for (Integer value : data.getValueList()) {
            builder.addValue(value);
        }
        for (OperaMaskBlock block : data.getBlockList()) {
            builder.addBlockList(parseOperaMaskBlockMsg(block));
        }
        return builder;
    }

    public static OperaMaskProto.OperaMaskLightEffectMsg.Builder parseOperaMaskLightEffectMsg(OperaMaskSkillEffect data) {
        OperaMaskProto.OperaMaskLightEffectMsg.Builder builder = OperaMaskProto.OperaMaskLightEffectMsg.newBuilder();
        builder.setSkillId(data.getSkillId());
        for (OperaMaskBlock block : data.getBlockList()) {
            builder.addBlockList(parseOperaMaskBlockMsg(block));
        }
        return builder;
    }

    /**
     * 转换客户端的颜色
     */
    public static int getClientColor(OperaMaskBlock block) {
        if (block.getType() == eOperaMaskBlockType.BombSuperColorful.getValue()) {
            return 101;
        }
        if (block.getType() == eOperaMaskBlockType.BombColorful.getValue()) {
            return 100;
        }
        if (block.getColor() == 0) {
            return 0;
        }
        if (block.getType() == eOperaMaskBlockType.BombShiZi.getValue()) {
            return block.getColor() + 30;
        }
        if (block.getType() == eOperaMaskBlockType.BombShu.getValue()) {
            return block.getColor() + 20;
        }
        if (block.getType() == eOperaMaskBlockType.BombHeng.getValue()) {
            return block.getColor() + 10;
        }
        return block.getColor();
    }


    /**
     * 获取客户端的爆炸类型
     */
    public static int getClientBoomType(OperaMaskBoom boom) {
        if (boom.getType() == eOperaMaskBoomType.ColorSingle.getValue()) {
            return eOperaMaskBoomType.ColorAndNormal.getValue();
        }
        return boom.getType();
    }
}
